﻿namespace Example
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Windows.Forms;
    using Example.Runtime.InteropServices;
    using mshtml;

    public partial class WebBrowserForm : Form
    {
        public WebBrowserForm()
        {
            InitializeComponent();

            _webBrowser.Navigate(new Uri("http://alan-dean.com/about.en.html"));
        }

        /// <summary>Gets an image of the currently displayed browser page.</summary>
        /// <param name="browser">The web browser instance.</param>
        /// <returns>An image of the currently displayed browser page.</returns>
        private Image GetCurrentPageImage(WebBrowser browser)
        {
            Image image = null;

            if (null != browser)
            {
                var document = browser.Document;
                if (null != document)
                {
                    var dom = document.DomDocument as IHTMLDocument2;
                    if (null != dom)
                    {
                        var element = dom.body;
                        if (null != element)
                        {
                            var render = element as Runtime.InteropServices.IHTMLElementRender;
                            if (null != render)
                            {
                                using (var graphics = CreateGraphics())
                                {
                                    var destination = graphics.GetHdc();
                                    render.DrawToDC(destination);
                                    var memory = NativeMethods.CreateCompatibleDC(destination);
                                    var bitmap = NativeMethods.CreateCompatibleBitmap(
                                        destination,
                                        browser.ClientRectangle.Width,
                                        browser.ClientRectangle.Height);

                                    if (IntPtr.Zero != bitmap)
                                    {
                                        var old = NativeMethods.SelectObject(memory, bitmap);
                                        NativeMethods.BitBlt(
                                            memory,
                                            0,
                                            0,
                                            browser.ClientRectangle.Width,
                                            browser.ClientRectangle.Height,
                                            destination,
                                            0,
                                            0,
                                            TernaryRasterOperations.Srccopy);
                                        NativeMethods.SelectObject(memory, old);
                                        NativeMethods.DeleteDC(memory);
                                        graphics.ReleaseHdc(destination);
                                        image = Image.FromHbitmap(bitmap);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return image;
        }

        /// <summary>
        /// Occurs when the <see cref="T:System.Windows.Forms.WebBrowser"/> control finishes loading a document.
        /// </summary>
        /// <param name="sender">The source <see cref="T:System.Windows.Forms.WebBrowser"/> control.</param>
        /// <param name="e">Any event parameters.</param>
        /// <seealso href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted.aspx">MSDN</seealso>
        private void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            using (var largeImage = GetCurrentPageImage(sender as WebBrowser))
            {
                largeImage.Save(new FileInfo("large.png").FullName, ImageFormat.Png);

                using (Image smallImage = new Bitmap(160, 120))
                {
                    using (var g = Graphics.FromImage(smallImage))
                    {
                        g.DrawImage(largeImage, 0, 0, 160, 120);

                        smallImage.Save(new FileInfo("small.png").FullName, ImageFormat.Png);
                    }
                }
            }

            Close();
        }
    }
}